package cn.bulibuqi.myblog.service;

import cn.bulibuqi.myblog.bean.Blog;
import cn.bulibuqi.myblog.bean.Tag;
import cn.bulibuqi.myblog.dao.BlogDao;
import cn.bulibuqi.myblog.dao.MyBlogMapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author bulibuqi
 * @create 2021-12-12:33
 */
@Service
public class BlogServiceImpl extends ServiceImpl<MyBlogMapper, Blog> implements BlogService{
    @Autowired
    BlogDao blogDao;

    @Override
    public Blog getBlog(Long id) {

        Blog allBlogInfoById = baseMapper.getAllBlogInfoById(id);

        return allBlogInfoById;
    }

    @Override
    public Page<Blog> listBlog(Page pageInfo, Blog blog) {

        QueryWrapper<Blog> wrapper = new QueryWrapper<>();

        wrapper.eq("1",1);

        //匹配TypeID
        wrapper.eq(blog.getTypeId()!=null,"blog.type_id",blog.getTypeId());

        List<Tag> tags = blog.getTags();
        //匹配Tag
        if (tags!=null && tags.size()!=0)
        {
            wrapper.and(s->{

                for (Tag tag : tags) {
                    s.or().eq(tag!=null,"blog_tag.tag_id",tag);

                }

            });

        }

        //匹配Title
        wrapper.eq(blog.getTitle()!=null,"blog.title",blog.getTitle());
        //匹配flag
        wrapper.eq(blog.getFlag()!=null,"blog.flag",blog.getFlag());
        //匹配appreciation
        wrapper.eq(blog.getAppreciation()!=null,"blog.appreciation",blog.getAppreciation());
        //匹配shareStatement
        wrapper.eq(blog.getShareStatement()!=null,"blog.share_statement",blog.getShareStatement());
        //匹配published
        wrapper.eq(blog.getPublished()!=null,"blog.published",blog.getPublished());
        //匹配recommend
        wrapper.eq(blog.getRecommend()!=null,"blog.recommend",blog.getRecommend());
        //匹配commentabled
        wrapper.eq(blog.getCommentabled()!=null,"blog.commentabled",blog.getCommentabled());
        //匹配userId
        wrapper.eq(blog.getUserId()!=null,"blog.userId",blog.getUserId());


        Page<Blog> pageBlogInfo = baseMapper.getPageBlogInfo(pageInfo, wrapper);


        return pageBlogInfo;


    }

    @Override
    public void saveBlog(Blog blog) {

        // 加入日期

        blog.setCreateTime(new Date());
        blog.setUpdateTime(new Date());
        blog.setViews(0L);
        //保存
       blogDao.saveBlogPlus(blog);

    }


    //没有写完 需求没有确定
    @Override
    public boolean updateBlog(Long id, Blog blog) {

        blog.setId(id);
        boolean b = updateById(blog);

        return b;

    }

    @Override
    public boolean deleteBlog(Long id) {

        boolean b = blogDao.deleteBlogPlus(id);
        return b;
    }

    @Override
    public void saveAndUpdate(Blog blog) {
        blogDao.saveAndUpdate(blog);

    }
}
